﻿using Core.Framework;
using System.Diagnostics;
using System.Reflection;

namespace System
{
    public static class ShellHelper
    {
        public static string Bash(this string cmd)
        {
            var escapedArgs = cmd.Replace("\"", "\\\"");

            var process = new Process()
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "/bin/bash",
                    Arguments = $"-c \"{escapedArgs}\"",
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                }
            };
            process.Start();
            string result = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            return result;
        }
    }

    public static class Prototype
    {
        /// <summary>
        /// 获取备注信息
        /// </summary>
        /// <returns></returns>
        public static string GetRemarkOfInfo<T>(this T tIn)
        {
            Type type = typeof(Enum);

            if (tIn.GetType().BaseType.Equals(type))
            {
                FieldInfo field = tIn.GetType().GetField(tIn.ToString());
                RemarkAttribute attribute =
                    Attribute.GetCustomAttribute(field, typeof(RemarkAttribute)) as RemarkAttribute;
                return attribute.Info;
            }

            object[] objAttrs = tIn.GetType().GetCustomAttributes(typeof(RemarkAttribute), true);
            if (objAttrs.Length > 0)
                return (objAttrs[0] as RemarkAttribute).Info;
            return string.Empty;
        }
    }
}
